package com.ufaud.ais.sys.webapp.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ufaud.ais.pub.service.IGenericCRUDService;
import com.ufaud.ais.pub.utils.StringUtil;
import com.ufaud.ais.sys.entity.Department;
import com.ufaud.ais.sys.entity.Employee;
import com.ufaud.ais.sys.entity.Role;
import com.ufaud.ais.sys.service.IDepartmentService;
import com.ufaud.ais.sys.service.IEmployeeService;

/**
 * 员工的action。
 * 
 * @author Chenjd
 * @version 最近的一次更新 2010-7-15
 * 
 */
public class EmployeeAction {

	private final static Log log = LogFactory.getLog(EmployeeAction.class);

	private String deptId;
	private List<Employee> employeeList = new ArrayList<Employee>();
	private List<Department> deptList = new ArrayList<Department>();
	private List<Role> roleList = new ArrayList<Role>();

	private IEmployeeService employeeService;
	private IDepartmentService deptService;
	private IGenericCRUDService<Role, Long> roleService;

	private String employeeId;

	/**
	 * 
	 * @author Chenjd
	 * @version 最近的一次更新 2010-7-20 下午04:57:00
	 * @return
	 */
	public String toList() {
		return success();
	}

	/**
	 * 查找部门下的员工。
	 * 
	 * @author Chenjd
	 * @version 最近的一次更新 2010-7-15 下午03:46:26
	 * @return
	 */
	public String findEmployeeInDept() {
		log.debug("find employees in deptId:" + deptId);
		if (deptId != null) {
			if (StringUtil.isNumberic(deptId)) {
				Long deptIdl = Long.parseLong(deptId);
				employeeList = findEmployeeInDept(deptIdl);
			}
		}
		return success();
	}

	private List<Employee> findEmployeeInDept(Long deptId) {
		List<Employee> employeeList = new ArrayList<Employee>();
		employeeList = employeeService.getEmployeeAccordToDeptId(deptId);
		log.debug("employee list size:" + employeeList.size());
		return employeeList;
	}

	/**
	 * 
	 * @author Chenjd
	 * @version 最近的一次更新 2010-7-23 下午08:37:06
	 * @return
	 */
	public String add() {
		boolean detpPrepared = prepareDeptList();
		boolean rolePrepared = prepareRoleList();
		if (!detpPrepared || !rolePrepared) {
			return fail();
		}
		return success();
	}

	public String remove() {
		Long employeeIdL = Long.parseLong(employeeId);
		Employee employee = employeeService.get(employeeIdL);
		employeeService.deleteEmployeeById(employeeIdL);
		employeeList = findEmployeeInDept(employee.getDepartment().getDeptId());
		return success();
	}

	private boolean prepareDeptList() {
		deptList = deptService.getAll();
		return true;
	}

	private boolean prepareRoleList() {
		roleList = roleService.getAll();
		return true;
	}

	private String fail() {
		return "fail";
	}

	private String success() {
		return "success";
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public List<Employee> getEmployeeList() {
		return employeeList;
	}

	public List<Department> getDeptList() {
		return deptList;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public void setDeptService(IDepartmentService deptService) {
		this.deptService = deptService;
	}

	public void setRoleService(IGenericCRUDService<Role, Long> roleService) {
		this.roleService = roleService;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

}
